<?php
/**
 * @desc 商品数据层
 * @author kevin.wang
 * @date 2015-01-01 12:00:00
 */
class Goods extends Base
{
	//数据表
	protected $table = 'pert_goods';
	protected $ctable = 'pert_goods_category';
	//获取商品分类
	public function getCategory()  
    {
		 $table = $this->ctable;
		 $filed = "`goods_category_id`,`parent_id`,`name`,`depth`";
		 $sql = "select {$filed} from `{$table}` where `delete_flag` = 0 order by `sort` desc, `goods_category_id` desc";
		 return $this->select_table($sql, array());
    }
	//获取商品列表
	public function getGoodsList($theme_id , $list_num = 24){
		$table = $this->table;
		$filed = "`goods_id`,`goods_sn`,`goods_name`,`goods_category_id`,`theme_id`,`goods_sub`,`price`,`unit`,`goods_num`,`sale_num`,`image`,`goods_intro`,`const_intro`,`attentions`,`asking`,`sort`,`detail_image`";
		$sql = "select {$filed} from `{$table}` where `theme_id` in ({$theme_id}) and `delete_flag` = 0 and `is_show` = 0 order by `sort` desc, `goods_id` desc limit {$list_num}";
		return $this->select_table($sql, array());
	}
	//获取商品详情
	public function getGoodsDetail($goods_sn){
		$table = $this->table;
		$filed = "`goods_id`,`goods_sn`,`goods_name`,`goods_category_id`,`theme_id`,`goods_sub`,`price`,`unit`,`goods_num`,`sale_num`,`image`,`goods_intro`,`const_intro`,`attentions`,`asking`,`sort`,`detail_image`";
		$sql = "select {$filed} from `{$table}` where `goods_sn` = :goods_sn and `delete_flag` = 0";
		return $this->select_table($sql, array(':goods_sn' => $goods_sn), Base::QUERY_RAW);
	}
	//商品分类
	public function goodsCategory($goods, $category){
		$result = $goods;
		if(empty($category)){
			return $result;
		}
		$result = array();
		foreach($category as $value){
			$result[$value['goods_category_id']] = array();
		}
		foreach($goods as $value){
			$result[$value['goods_category_id']][] = $value;
		}
		return $result;
	}
}
